<%@ page import="test.CalculatorComponent" %>
<%@ taglib uri="http://bhf.org/taglibs/bhs" prefix="bhs" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>BHF JSON Component Tests</title>
</head>
<body>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>

    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/logger/logger-min.js"></script>
    <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yuitest/yuitest.js"></script>

    <script type="text/javascript" src="bhf-services.js"></script>
    <script type="text/javascript" src="bhf-components.js"></script>

    
    <script type="text/javascript">
        var aCalculator;
        var Assert = YAHOO.util.Assert;

        //
        // Pass One: make the calls
        //
        YAHOO.tool.TestRunner.add( new YAHOO.tool.TestCase({
            name: "Test Component Events",

            setUp: function()
            {
                <% CalculatorComponent calculator = new CalculatorComponent(); request.setAttribute( "calculator", calculator );%>
                aCalculator = <bhs:component component="${calculator}" absolute="true" />;
            },

            testComplexEvent: function()
            {
                var that = this;

                aCalculator.events.echo.subscribe( function( type, y ) { that.resume( function() {
                    Assert.areEqual( 5, y[0].real );
                    Assert.areEqual( 8, y[0].imaginary );
                })});
                aCalculator.state.currentValue = 5;
                aCalculator.echoWithEventComplex(
                    {
                        success: function() { },
                        failure: function() { that.resume( function() { Assert.fail( "Exception not expected" ); } ); }
                    },
                    { real: 5, imaginary: 8 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testVoidEvent: function()
            {
                var that = this;

                aCalculator.events.update.subscribe( function() { that.resume(); } );
                aCalculator.state.currentValue = 5;
                aCalculator.add(
                    {
                        success: function() { },
                        failure: function() { that.resume( function() { Assert.fail( "Exception not expected" ); } ); }
                    },
                    3
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testPrimitiveEvent: function()
            {
                var that = this;

                aCalculator.events.echo.subscribe( function( type, y ) { that.resume( function() {
                    Assert.areEqual( "Yahoo!", y[0] );
                })});
                aCalculator.state.currentValue = 5;
                aCalculator.echoWithEventString(
                    {
                        success: function() { },
                        failure: function() { that.resume( function() { Assert.fail( "Exception not expected" ); } ); }
                    },
                    "Yahoo!"
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            },

            testMultiEvent: function()
            {
                var that = this;

                aCalculator.events.echo.subscribe( function( type, y ) { that.resume( function() {
                    Assert.areEqual( 5, y[0] );
                    Assert.areEqual( 8, y[1] );
                })});
                aCalculator.state.currentValue = 5;
                aCalculator.echoWithEventComplexParts(
                    {
                        success: function() { },
                        failure: function() { that.resume( function() { Assert.fail( "Exception not expected" ); } ); }
                    },
                    { real: 5, imaginary: 8 }
                );

                this.wait( function()
                    {
                        Assert.fail( "Timed out" );
                    }, 5000
                );
            }
        }));

        YAHOO.tool.TestManager.load();
    </script>
</body>
</html>